#!/usr/bin/env node

// node cli 应用入口需要文件头 #!/usr/bin/env node
// linux 或者 mac 需要修改文件读写权限为 755 chmod 755 cli.js
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const ejs = require('ejs');

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'project name'
    }
]).then(anwsers => {
    console.log('anwsers', anwsers);
    // 模板目录
    const tmplDir = path.join(__dirname, 'templates');
    // 目标目录
    const distDir = process.cwd();
    
    // 模板转换
    fs.readdir(tmplDir, (err, files) => {
        if (err) throw err;
        files.forEach(file => {
            console.log('file', file);
            ejs.renderFile(path.join(tmplDir, file), anwsers, {}, (err, str) => {
                if (err) throw err;
                fs.writeFileSync(path.join(distDir, file), str);
            });
        });
    });
});